package hczc.longcai.com.hccrowdfunding.home.fourth_mine.activities;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import hczc.longcai.com.hccrowdfunding.R;
import hczc.longcai.com.hccrowdfunding.activity.BaseActivity;
import hczc.longcai.com.hccrowdfunding.application.BaseApplication;
import hczc.longcai.com.hccrowdfunding.home.fourth_mine.adapters.RedPacketAdapter;
import hczc.longcai.com.hccrowdfunding.home.fourth_mine.beans.RedPacket;

public class RedPacketActivity extends BaseActivity implements View.OnClickListener {

    private static final String TAG = "RedPacketActivity";
    private List<RedPacket> list;
    private ListView listView;
    private RedPacketAdapter adapter;
    private TextView money;

    private String[] type = {"参加活动","参加活动","参加活动"};
    private String[] time = {"2016-02-17","2016-02-17","2016-02-17"};
    private String[] sum = {"+100元","+520元","+888元"};

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_red_packet);

        initDatas();
        initViews();
    }

    private void initDatas() {
        list = new ArrayList<>();
        int length = type.length;
        for (int i = 0; i < length; i++){
            RedPacket redPacket = new RedPacket(type[i],time[i],sum[i]);
            list.add(redPacket);
        }
    }

    private int getNumber(String s){
        int i = 0;
        String regEx = "[^0-9]";
        Pattern p = Pattern.compile(regEx);
        Matcher m = p.matcher(s);
        String s1 = m.replaceAll("");
        i = Integer.valueOf(s1);
        Log.d(TAG,"红包金额："+s1);
        return i;
    }

    private String getRedMoney(){
        String money = "";
        int c_money = 0;
        int length = sum.length;
        for (int i = 0; i < length; i++){
            c_money += getNumber(sum[i]);
        }
        float cMoney = (float) c_money;
        String result = String.format("%.2f",cMoney);

        money = result + "元";


        return money;
    }

    private void initViews() {
        findViewById(R.id.head_back).setOnClickListener(this);
        ((TextView) findViewById(R.id.head_title)).setText("红包");
        TextView shuoming = (TextView) findViewById(R.id.head_right_text);
        shuoming.setText("说明");
        shuoming.setVisibility(View.VISIBLE);
        shuoming.setOnClickListener(this);

        money = ((TextView) findViewById(R.id.red_packet_money));
        String redMoney = getRedMoney();
        money.setText(redMoney);

        View view = LayoutInflater.from(this).inflate(R.layout.header_red_packet, null);
        BaseApplication.scaleScreenHelper.loadView(((ViewGroup) view));

        listView = ((ListView) findViewById(R.id.mine_red_packet_listview));
        listView.addHeaderView(view,null,false);
        adapter = new RedPacketAdapter(this,list);
        listView.setAdapter(adapter);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id){
            case R.id.head_back:
                finish();
                break;
            case R.id.head_right_text:
                startActivity(new Intent(RedPacketActivity.this,RedPacketExplainActivity.class));
                break;
        }
    }
}
